import {shopping} from '@/request/api';
export default{
    namespaced:true,
    state: {
      // 购物车全局数据
      gwcvalue: 7,
      list:[],
      nextid:7,
      firstzx:true
    },
    // 数据方法
    mutations: {
      // 首次执行函数
      notfirstzx(state){
        state.firstzx = false;
      },
      changelist(state,list){
        state.list = list;
      },
      changegwc(state,value){
        state.gwcvalue = value;
      },
       // 根据id删除对的任务
      removeitem(state, id) {
        // 根据id查找对应项的检索
        let i = state.list.findIndex((x) => x.id === id);
        // 根据索引删除对应的元素
        if (i !== -1) {
          state.list.splice(i, 1);
          localStorage.setItem("bdlist", JSON.stringify(state.list));
        }
      },
      // 添加购物车
      additem(state,cobj) {
        const obj = {
          id: JSON.stringify( state.nextid ),
          imga: cobj.bimga ,
          title:cobj.btitle,
          cfav:cobj.bcfav,
          tradeItemId:cobj.btradeItemId,
          type:cobj.btype
        };
        state.list.push(obj);
        // eslint-disable-next-line no-plusplus
        state.nextid++;
        localStorage.setItem("bdlist", JSON.stringify(state.list));
      }
    },
    // 异步改变全局数据
    actions: {
      async shopping(context){
        let res = await shopping();
        localStorage.setItem("bdlist", JSON.stringify(res.data));
        context.commit('changelist', res.data);
        // context.commit('changegwc', res.data.length);
      }      
    },
     // 全局数据的表达式
    getters: {
      // gwcvalue( ) {
      //   return JSON.parse(localStorage.getItem("bdlist")).length;
      // },
    },
}